from sqlalchemy import INT, Column, BigInteger, String, create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from goodDesignImage2018Spider.items import WaiMaiYuanItem
import json

# 对象的基类
Base = declarative_base()


class WaiMaiYuanItemOrm(Base):
    # 表名
    __tablename__ = 'waimaiyuan_item'
    # 主键id
    item_id = Column(BigInteger(), primary_key=True)
    # 名称
    item_name = Column(String(100), default='')
    # 父id
    item_parent_id = Column(BigInteger(), default=-1)
    # 备注
    item_remark = Column(String(100), default='')
    # 业务类型 0菜谱分类
    item_biz_type = Column(INT())


#     初始化数据库连接
engine = create_engine('mysql+pymysql://root:Mysqlroot@localhost:3306/waimaiyuan')
# 创建DBSession类型
DBSession = sessionmaker(bind=engine)

if __name__ == '__main__':
    session = DBSession()

    # item2 = WaiMaiYuanItemOrm(item_id=2)
    # # add是插入
    # session.add(item2)
    # session.commit()

    item1 = session.query(WaiMaiYuanItemOrm).filter(WaiMaiYuanItemOrm.item_id == 1).one()

    print(item1.item_name)
    session.close()
